<?PHP
require "../../mainfile.php";

if (!isset ($_POST['action']) && !isset ($_GET['action'])) {
	header("Location:index.php");
}
elseif (isset ($_POST['action'])) {
	$action = $_POST['action'];
}
elseif (isset ($_GET['action'])) {
	$action = $_GET['action'];
}

//##########################################################################################
//##########################################################################################
//#################################   AJAX PREVIEW IN ADMIN   ##############################
//##########################################################################################
if ($_GET['action'] == 'getStory') { //

	//GET INFO FROM DB
	if ($_GET['syndicated'] == 'false') {
		//-------------------------------------------------------------------
		$result = $xoopsDB->query("SELECT * FROM " . $xoopsDB->prefix("local_news_stories") . " WHERE ID = '" . $_GET['ID'] . "'");
		if ($result) {
			$myrow = $xoopsDB->fetchArray($result);
		}
		//-------------------------------------------------------------------
	} else { //get from Syndicated DB
		//-------------------------------------------------------------------
		$link = mysql_connect($xoopsModuleConfig['syndicationDB_Host'], $xoopsModuleConfig['syndicationDB_User'], $xoopsModuleConfig['syndicationDB_Passaword']) or ($error = mysql_error());
		$Query = "SELECT * FROM CA_SE_News WHERE ID = '" . $_GET['ID'] . "'";
		mysql_select_db($xoopsModuleConfig['syndicationDB']) or ($error = "Could not select database");
		$result = mysql_query($Query, $link) or ($error = "Query failed");

		if ($result) {
			$myrow = mysql_fetch_array($result, MYSQL_ASSOC);
		}
		mysql_free_result($result);
		mysql_close($link);

		//-------------------------------------------------------------------
	}

	//#####################
	//STORY LOGIC GOES HERE
	function getSoundFile($sound) {
		global $xoopsModule;
		if (!ereg('http', $sound)) {
			return XOOPS_URL_NON_SSL . "/modules/" . $xoopsModule->getVar('dirname') . "/media/audio/" . $sound;
		} else {
			return $sound;
		}
	} //end function 

	function getContactFormURL($path) {
		global $xoopsModule;
		if (!ereg('http', $path)) {
			return XOOPS_URL;
		} else {
			return substr($path, 0, 15);
		}

	}

	function getImageUrl($image, $TN = false) {
		global $xoopsModule;
		if (!ereg('http', $image) && $TN == false) {
			return XOOPS_URL . "/modules/" . $xoopsModule->getVar('dirname') . "/media/pictures/" . $image;
		}
		elseif ($TN == false) {
			return $image;
		}
		elseif (!ereg('http', $image) && $TN == true) {
			return XOOPS_URL . "/modules/" . $xoopsModule->getVar('dirname') . "/media/pictures/" . substr_replace($image, '-TN.jpg', -4);
		} else {
			return substr_replace($image, '-TN.jpg', -4);
		}
	}
	
	
	function getCopyright($date,$copyright){
	  	if($copyright){
	  		return "&copy; Copyright ".date("Y",$date).", ".$copyright;
	  	}else{
	  		return "&nbsp;";
	  	}
	  		  	
	 }
	
	$myts = & MyTextSanitizer :: getInstance();

	if ($myrow['image'] == "") { //NO IMAGE
		echo "<DIV CLASS='headline-large' id='headlinePreview'>" . $myrow['headline'] . "</DIV>
						 <div id='bylinePreview'>" . $myrow['byline'] . "</div>";
		if ($myrow['soundfile']) {
			echo "<div id='soundClip' style='margin-top:2px;'><!--BEGIN MP3 PLAYER--><object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='300' height='75' id='flashID' align='middle'><param name='allowScriptAccess' value='sameDomain' /><param name='movie' value='" . XOOPS_URL . "/modules/local_news/media/mp3Player.swf' /><param name='quality' value='high' /><param name='bgcolor' value='#ffffff' /><PARAM NAME=FlashVars VALUE='file=" . getSoundFile($myrow['soundfile']) . "&autoPlayStart=true&flashID=flashID'><embed src='" . XOOPS_URL . "/modules/local_news/media/mp3Player.swf' FlashVars='file=" . getSoundFile($myrow['soundfile']) . "&autoPlayStart=true&flashID=flashID' quality='high' bgcolor='#ffffff' width='300' height='75' name='flashID' align='middle' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' /></object><!--END MP3 PLAYER--></div>";
		}
		echo " <div id='container' style='margin-top:10px;margin-bottom:10px;'>" . $myrow['dateline'] . " (" . date("Y-m-d", $myrow['publish_date']) . ") " . $myts->displayTarea($myrow['tease']);
		if ($myrow['body']) {
			echo " <a href='javascript:void(0);' onClick=\"addBody(); \" id='moreLink'>more...</a><div id='bodyHolder' style='display:none;'><br />" . trim(html_entity_decode($myrow['body'])) . "</div>";
		}
		echo "<div id='copy' style='padding-top:5px;' CLASS='article-small' >" .getCopyright($myrow['publish_date'],$myrow['copyright']). "</div>
						</div>";
	} else { //GOT AN IMAGE
		echo "<DIV CLASS='headline-large' id='headlinePreview'>" . $myrow['headline'] . "</DIV>
						 <div id='bylinePreview'>" . $myrow['byline'] . "</div>";
		if ($myrow['soundfile']) {
			echo "<div id='soundClip' style='margin-top:2px;'><!--BEGIN MP3 PLAYER--><object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='300' height='75' id='flashID' align='middle'><param name='allowScriptAccess' value='sameDomain' /><param name='movie' value='" . XOOPS_URL . "/modules/local_news/media/mp3Player.swf' /><param name='quality' value='high' /><param name='bgcolor' value='#ffffff' /><PARAM NAME=FlashVars VALUE='file=" . getSoundFile($myrow['soundfile']) . "&autoPlayStart=true&flashID=flashID'><embed src='" . XOOPS_URL . "/modules/local_news/media/mp3Player.swf' FlashVars='file=" . getSoundFile($myrow['soundfile']) . "&autoPlayStart=true&flashID=flashID' quality='high' bgcolor='#ffffff' width='300' height='75' name='flashID' align='middle' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' /></object><!--END MP3 PLAYER--></div>";
		}
		echo " <div id='container' style='margin-top:10px;margin-bottom:10px;'>
							 <div style='width:" . $myrow['imageTNwidth'] . "px;float:left;padding-right:5px;padding-bottom:5px;' >
								<div id='image'><a href='" . getImageUrl($myrow['image']) . "' TARGET='_blank'> <img src='" . getImageUrl($myrow['image'], true) . "' style='margin-left:0; float:left; margin-right:3px; margin-bottom:0px; border:1px solid #000000;'></a></div>";
		if ($myrow['credit']) {
			echo "<div id='credit' CLASS='photo-credit' style='width:" . $myrow['imageTNwidth'] . "px;'>Photo By:" . $myrow['credit'] . "</div>";
		}
		echo "<div id='caption' CLASS='photo-caption' style='width:" . $myrow['imageTNwidth'] . "px;'>" . $myrow['caption'] . "</div>
							</div>
							" . $myrow['dateline'] . " (" . date("Y-m-d", $myrow['publish_date']) . ") " . $myts->displayTarea($myrow['tease']);
		if ($myrow['body']) {
			echo " <a href='javascript:void(0);' onClick=\"addBody(); \" id='moreLink'>more...</a><div id='bodyHolder' style='display:none;'><br />" . trim(html_entity_decode($myrow['body'])) . "</div>";
		}
		echo "<div id='copy' style='padding-top:5px;' CLASS='article-small' >" .getCopyright($myrow['publish_date'],$myrow['copyright']). "</div>
						</div>";
	}

	echo "<div style='margin-top:7px;'><a href='javascript:void(0);' onClick=\"emailStory(" . $myrow['ID'] . "," . $_GET['syndicated'] . ")\"><img alt='Email This Article to a Friend.' style='margin-left:0; margin-bottom:0;' src='" . XOOPS_URL . "/modules/local_news/images/email.gif' /></a> &nbsp; &nbsp; &nbsp; <a href='#' onClick=\"printStory(" . $myrow['ID'] . "," . $_GET['syndicated'] . ")\"><img alt='Print This Article' style='margin-left:0; margin-bottom:0;' src='" . XOOPS_URL . "/modules/local_news/images/print.gif' /></a>&nbsp; &nbsp; &nbsp; <a href='" . getSoundFile($myrow['soundfile']) . "'><img src='images/download.gif' alt='Download this News Story' /></a>&nbsp; &nbsp; &nbsp; <A HREF='" . getContactFormURL($myrow['soundfile']) . "/modules/contact_form/'><img src='images/news_tip.gif' alt='Submit Feedback or News Tip'/></A> </div>";

	//END STORY LOGIC
	//##################### 

}
?>